package com.qqtech.qquan.pub.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.activity.dao.ActivityBaseDao;
import com.qqtech.qquan.activity.model.ActivityBase;
import com.qqtech.qquan.circle.dao.CircleTopicDao;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.community.dao.CommunityMessageDao;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.idle.dao.IdleBaseDao;
import com.qqtech.qquan.idle.model.IdleBase;
import com.qqtech.qquan.pub.dao.PubReportDao;
import com.qqtech.qquan.pub.enums.PubReportStatusDictEnum;
import com.qqtech.qquan.pub.model.PubReport;
import com.qqtech.qquan.pub.model.PubReportQuery;
import com.qqtech.qquan.pub.model.PubReportVo;
import com.qqtech.qquan.user.dao.UserTopicDao;
import com.qqtech.qquan.user.model.UserTopic;

@Service
public class PubReportServiceImpl extends BaseServiceImpl<PubReport>implements PubReportService {
	public static final Logger log = LoggerFactory.getLogger(PubReportServiceImpl.class);

	@Resource
	private PubReportDao pubReportDao;
	@Resource
	private CircleTopicDao circleTopicDao;
	@Resource
	private CircleTopicDao CircleTopicDao;
	@Resource
	private CommunityMessageDao communityMessageDao;
	@Resource
	private UserTopicDao userTopicDao;
	@Resource
	private IdleBaseDao idleBaseDao;
	@Resource
	private ActivityBaseDao activityBaseDao;

	@Override
	protected BaseDao<PubReport> getBaseDao() {
		return pubReportDao;
	}

	@Override
	public String handleDisposeReport(Integer userId, Integer reportId, String bizzType, Integer bizzId) {
		String title = "";
		PubReport report = new PubReport();
		report.setId(reportId);
		report.setStatus(PubReportStatusDictEnum.DISPOSE.getCode());
		report.setHandleAt(TimeUtil.now());
		pubReportDao.updateById(report);

		// 修改业务单据状态
		if (BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(bizzType)) {//话题
			CircleTopic circleTopic = new CircleTopic();
			circleTopic.setId(bizzId);
			circleTopic.setIsShow(YesNoEnum.NO.getCode());// 不显示
			circleTopicDao.updateById(circleTopic);
		}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(bizzType)){ //大喇叭
			CommunityMessage communityMessage = new CommunityMessage();
			communityMessage.setId(bizzId);
			communityMessage.setIsShow(YesNoEnum.NO.getCode());// 不显示
			communityMessageDao.updateById(communityMessage);
		}else if(BizzLevel1Enum.IDLE.getCode().equals(bizzType)){ //交易信息
			IdleBase idleBase = new IdleBase();
			idleBase.setId(bizzId);
			idleBase.setIsShow(YesNoEnum.NO.getCode());// 不显示
			idleBaseDao.updateById(idleBase);
		}else if(BizzLevel1Enum.ACTIVITY.getCode().equals(bizzType)){ //活动
			ActivityBase activityBase = new ActivityBase();
			activityBase.setId(bizzId);
			activityBase.setIsShow(YesNoEnum.NO.getCode());// 不显示
			activityBaseDao.updateById(activityBase);
		}
		return title;
	}

	@Override
	public void handleIgnoreReport(Integer userId, Integer reportId, String bizzType, Integer bizzId) {
		PubReport report = new PubReport();
		report.setId(reportId);
		report.setStatus(PubReportStatusDictEnum.WAITING.getCode());
		report.setHandleAt(TimeUtil.now());
		pubReportDao.updateById(report);

		// 修改业务单据状态
		if (BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(bizzType)) {//话题
			CircleTopic circleTopic = new CircleTopic();
			circleTopic.setId(bizzId);
			circleTopic.setIsShow(YesNoEnum.YES.getCode());// 显示
			circleTopicDao.updateById(circleTopic);
		}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(bizzType)){ //大喇叭
			CommunityMessage communityMessage = new CommunityMessage();
			communityMessage.setId(bizzId);
			communityMessage.setIsShow(YesNoEnum.YES.getCode());// 显示
			communityMessageDao.updateById(communityMessage);
		}else if(BizzLevel1Enum.IDLE.getCode().equals(bizzType)){ //交易信息
			IdleBase idleBase = new IdleBase();
			idleBase.setId(bizzId);
			idleBase.setIsShow(YesNoEnum.YES.getCode());// 显示
			idleBaseDao.updateById(idleBase);
		}else if(BizzLevel1Enum.ACTIVITY.getCode().equals(bizzType)){ //活动
			ActivityBase activityBase = new ActivityBase();
			activityBase.setId(bizzId);
			activityBase.setIsShow(YesNoEnum.YES.getCode());//显示
			activityBaseDao.updateById(activityBase);
		}

	}
	
	@Override
	public Integer getBeUserId(int bizzId, String bizzType) {
		if(BizzLevel2Enum.CIRCLE_TOPIC.equals(bizzType)){
			CircleTopic circleTopic = CircleTopicDao.getById(bizzId);
			if(circleTopic != null){
				return circleTopic.getCreatorId();
			}
		}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.equals(bizzType)){
			CommunityMessage communityMessage = communityMessageDao.getById(bizzId);
			if(communityMessage != null){
				return communityMessage.getCreatorId();
			}
		}else if(BizzLevel2Enum.USER_TOPIC.equals(bizzType)){
			UserTopic userTopic = userTopicDao.getById(bizzId);
			if(userTopic != null){
				return userTopic.getCreatorId();
			}
		}else if(BizzLevel1Enum.IDLE.equals(bizzType)){
			IdleBase idleBase = idleBaseDao.getById(bizzId);
			if(idleBase != null){
				return idleBase.getCreatorId();
			}
		}
		return null;
	}
	
	@Override
	public Page<PubReportVo> queryPageListAdmin(PubReportQuery query, Pageable pageable) {
		return pubReportDao.queryPageListAdmin(query, pageable);
	}

}